import { ref, computed } from 'vue'
import { defineStore } from 'pinia'

export const useFormatTime = defineStore('formatTime', () => {

    function formatDate(timestamp, template = "YYYY-mm-DD ") {
        const date = new Date(timestamp);
        const year = date.getFullYear().toString();
        const month = (date.getMonth() + 1).toString().padStart(2, '0');
        const day = date.getDate().toString().padStart(2, '0');
        const hour = date.getHours().toString().padStart(2, '0');
        const minute = date.getMinutes().toString().padStart(2, '0');
        const second = date.getSeconds().toString().padStart(2, '0');
        // 获取星期几
        const weekday = date.getDay();
        const weekdays = ['星期日', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
        const weekdayStr = weekdays[weekday];

        const formattedDate = template
            .replace('YYYY', year)
            .replace('MM', month)
            .replace('DD', day)
            .replace('HH', hour)
            .replace('mm', minute)
            .replace('ss', second)
            .replace('WW', weekdayStr);

        return formattedDate;
    }

    return { formatDate }
})